//
//  DiarySubViewController.m
//  e家园设计
//
//  Created by 愉乐 on 15/4/14.
//  Copyright (c) 2015年 lanou3g.com. All rights reserved.
//

#import "DiarySubViewController.h"
#import "MyConnection.h"
#import "DiarySubViewCell.h"
#import "UIImageView+WebCache.h"
#import "ImageTableViewCell.h"
@interface DiarySubViewController ()<UITableViewDataSource, UITableViewDelegate, MyConnectionDelegate>

@end

@implementation DiarySubViewController

- (void)dealloc
{
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    [self creatTableView];
    [self creatHeaderView];
    self.navigationController.navigationBar.translucent = NO;
    //id=114214&page=1&source=main"
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.diaryId forKey:@"id"];
    [dic setObject:@"main" forKey:@"source"];
    [dic setObject:@"1" forKey:@"page"];
    [MyConnection connectWithUrl:details parmaters:dic delegate:self type:1];
}

- (void)receiveDataFinished:(NSData *)data type:(NSInteger)type
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSDictionary *dic2 = [dic objectForKey:@"data"];
    NSArray *array = [NSArray array];
    array = [dic2 objectForKey:@"contents"];
    
    for (int i = 0; i < [array count]; i++) {
        NSDictionary *dic3 = [array objectAtIndex:i];
        NSArray *array2 = [dic3 objectForKey:@"content"];
        [self.tableArray addObjectsFromArray:array2];
    }
    [self.tableView reloadData];
}


- (void)creatHeaderView
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 200)];
    [self.tableView addSubview:imageView];
    [self.tableView release];
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 200;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiarySubViewCell *cell = (DiarySubViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * a = @"1";
    static NSString * b = @"2";
    DiarySubViewCell *textCell = [tableView dequeueReusableCellWithIdentifier:a];
    ImageTableViewCell *imageCell = [tableView dequeueReusableCellWithIdentifier:b];
    [textCell setNeedsUpdateConstraints];
    [textCell updateConstraintsIfNeeded];
    
    if (textCell != nil) {
        textCell = [[[DiarySubViewCell alloc] init] autorelease];
    }

    if (!textCell) {
        textCell = [[DiarySubViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:a];
    }
    
    if (!imageCell) {
        imageCell = [[ImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:b];
    }
  
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    if ([[dic objectForKey:@"type"] isEqualToString:@"desc"]) {
        [textCell setTextOfMaterialLabel:[dic objectForKey:@"data"]];
        return textCell;
    }
    
    
    NSDictionary *dic2 = [dic objectForKey:@"data"];
    NSURL *url = [NSURL URLWithString:[dic2 objectForKey:@"image"]] ;
    [imageCell.aimage sd_setImageWithURL:url];

    

    return imageCell;

    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
